﻿<?php
	include "../common/db.inc";
	$mysqli = new mysqli($host, $username, $password,$dbname);
	$mysqli->set_charset('utf8');	

	if (mysqli_connect_errno()) {
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}

	$userId = $_GET['user'];	
	$ebookId = $_GET['ebook'];
	$point = $_GET['point'];
	$query = ("call sp_AddRating('$userId', '$ebookId', '$point')");
	if ($result = $mysqli->query($query)) 
	{
		$mysqli = new mysqli($host, $username, $password,$dbname);
		$query = ("call sp_GetRatingPoint('$ebookId')");
		$result = $mysqli->query($query);
		$row = mysqli_fetch_row($result);
		$rating = $row[0];
		$a = floor($rating);
		$b = $rating - $a;
		
		$vote = 0;
		$mysqli = new mysqli($host, $username, $password,$dbname);
		$query = "call sp_GetVote('$ebookId')";
		$result = $mysqli->query($query);
		if(mysqli_num_rows($result) != 0)
		{
			$row = mysqli_fetch_row($result);
			$vote = $row[0];
		}
		
		echo "<b class='left'>Đánh giá: </b>";
		echo "<div id='star'>";
		for($j=0; $j<$a; $j++)
			echo "<img src='../images/star_on.png' />";
		for($j=0; $j<$b; $j++)
			echo "<img src='../images/star_half.png' />";
		for($j=0; $j<5-ceil($rating); $j++)
			echo "<img src='../images/star_off.png' />";
		echo "</div>";
		echo "<label>&nbsp;&nbsp;($vote phiếu)</label>";
	}
?>
